/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pgentity;

import db.DBContext;
import db.RedisKey;
import java.util.HashMap;
import java.util.Map;
import org.json.simple.JSONValue;
import pgentity.quest.QuestState;
import share.AMFBuilder;
import share.PGHelper;
import share.PGMacro;

/**
 *
 * @author Salm
 */
public class UQPack implements PGEntity {
    private final String uid;
    private final int index;

    private QuestState state;
    private Object action;
    private Map<String, Object> prize;

    private final Map<String, String> newValues = new HashMap();

    private UQPack(String uid, int index) {
        this.uid = uid;
        this.index = index;
    }

    public static UQPack getUQPack(String uid, int index)
    {
        UQPack pack = new UQPack(uid, index);
        pack.updateFromDB();

        return pack;
    }

    public static RedisKey packKey(String uid, int index)
    {
        return UrgentQuest.key(uid).getChild(String.valueOf(index));
    }
    
    public static void destroy(String uid, int index)
    {
        DBContext.Redis().del(packKey(uid, index));
    }

    @Override
    public void updateFromDB() {
        Map<String, String> data = DBContext.Redis().hgetall(packKey(uid, index));
        this.fillFromData(data);
    }

    private void fillFromData(Map<String, String> data)
    {
        this.state = QuestState.parse(data.get(PGMacro.UQ_PACK_STATE));

        String actJson = data.get(PGMacro.UQ_PACK_ACTION);
        this.action = ((PGHelper.isNullOrEmpty(actJson))?null:JSONValue.parse(actJson));

        String pzJson = data.get(PGMacro.UQ_PACK_PRIZE);
        this.prize = (Map) ((PGHelper.isNullOrEmpty(pzJson))?null:JSONValue.parse(pzJson));
    }

    @Override
    public void saveToDB() {
        if (!newValues.isEmpty())
        {
            DBContext.Redis().hset(packKey(uid, index), newValues);
            newValues.clear();
        }
    }

    public QuestState getState() {
        return state;
    }

    public void setState(QuestState state) {
        if (state != this.state)
        {
            this.state = state;
            newValues.put(PGMacro.UQ_PACK_STATE, String.valueOf(state));
        }
    }

    public Object getAction() {
        return action;
    }

    public void setAction(Object action) {
        this.action = action;
        newValues.put(PGMacro.UQ_PACK_ACTION, JSONValue.toJSONString(action));
    }

    public Map<String, Object> getPrize() {
        return prize;
    }

    public void setPrize(Map<String, Object> prize) {
        this.prize = prize;
        newValues.put(PGMacro.UQ_PACK_PRIZE, JSONValue.toJSONString(prize));
    }

    public Object buildAMF()
    {
        Map<String, Object> data = (Map) buildActionAMF();
        data.put(PGMacro.UQ_PACK_PRIZE, prize);
        return data;
    }
    
    public Object buildActionAMF()
    {
        Map<String, Object> data = new HashMap();
        data.put(PGMacro.UQ_PACK_STATE, state.toString());
        data.put(PGMacro.UQ_PACK_ACTION, action);
        return data;
    }
}
